//
// Created by Jisam on 23/10/2024 22:08.
// Solution of  template
#include <bits/stdc++.h>

#define int long long
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define pb push_back
using namespace std;

signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<pair<int, int>> a(6);
    for (int i = 0; i < 6; i++)
    {
        cin >> a[i].first >> a[i].second;
        a[i].second += 3;
    }
    sort(all(a), [&](pair<int, int> p1, pair<int, int> p2) -> bool
    {
        return p1.first < p2.first;
    });

    int time = a[0].first -  0;
    int t1 = a[0].first, t2 = a[0].second;

    for(int i = 1 ; i < 6 ;i ++){
        if(t2  >= a[i].first){
            t2 = min(a[i].second,n);
        }else if(t1 <= a[i].first && t2 >= a[i].second){
            continue;
        }else if(a[i].first - t2 > 0){
            int t = min(n,a[i].first);
            time += t - t2 - 1;
            t1 = a[i].first;    
            t2 = min(a[i].second,n);
        }
        cerr << time << " " << t1 << " " << t2 << endl;
    }
    
    cout << time;
    return 0;
}